'''
Created on 06/04/2009

@author: Edward
'''
from yarra.service.timer.timerservicelistener import TimerServiceListener
from yarra.service.timer.timerservicequeue import TimerServiceQueue

class TimerServiceManager(object):
    '''
    classdocs
    '''

    __timer_queue = None
    __timer_listener = None
    def __init__(self):
        '''
        Constructor
        '''        
        # constructor runs each time __new__ returns a instance, so we
        # have a check to make sure we don't initialize again. YES its weird
        # that even though the same instance is returned, it still runs
        # this __init__ method
        if self.__timer_listener is None and self.__timer_queue is None:
            self.__timer_queue = TimerServiceQueue()
            self.__timer_listener = TimerServiceListener(self.__timer_queue)
    
    def __new__(cls, *args, **kwargs): 
        if not hasattr(cls, 'self'):
            cls.self = object.__new__(cls, *args, **kwargs)
        return cls.self
    
    def add_timer_event(self, timestamp, event):
        self.__timer_queue.put(timestamp, event)

    def start(self):        
        self.__timer_listener.start()
    
    def stop(self):
        self.__timer_listener.stop_listener()        